package com.gsp.gspframework.helper;

import com.gsp.gspframework.annotation.Inject;
import com.gsp.gspframework.util.ReflectionUtil;

import java.lang.reflect.Field;
import java.util.Arrays;
import java.util.Map;

public final class IocHelper {

    static {
        Map<Class<?>, Object> beanMap = BeanHelper.getBeanMap();
        beanMap.forEach((clz, instance) -> {
            Field[] fields = clz.getDeclaredFields();
            Arrays.stream(fields).forEach(field -> {
                if (field.isAnnotationPresent(Inject.class)) {
                    Object o = BeanHelper.getBean(field.getType());
                    if (o != null)
                        ReflectionUtil.setField(instance, field, o);
                }
            });
        });
    }
}
